// 模型相关类型定义

export type ModelType = 'KnowledgeQA' | 'Embedding' | 'Rerank'

export type ModelStatus = 'active' | 'downloading' | 'inactive'

export type ModelSource = 'local' | 'cloud'

// 嵌入模型参数
export interface EmbeddingParameters {
  dimension: number
  truncate_prompt_tokens: number
}

// 模型参数
export interface ModelParameters {
  base_url: string
  api_key: string
  embedding_parameters?: EmbeddingParameters
}

// 模型项
export interface ModelItem {
  id: string
  tenant_id: number
  name: string
  type: ModelType
  source: ModelSource
  description: string
  parameters: ModelParameters
  is_default: boolean
  status: ModelStatus
  created_at: string
  updated_at: string
  deleted_at: string | null
}

// 创建模型请求
export interface CreateModelRequest {
  name: string
  type: ModelType
  source: ModelSource
  description: string
  parameters: ModelParameters
  is_default: boolean
}

// 更新模型请求
export interface UpdateModelRequest {
  name?: string
  description?: string
  parameters?: ModelParameters
  is_default?: boolean
}

// API响应类型
export interface ApiResponse<T = any> {
  success: boolean
  data: T
  message?: string
}

// 分页响应类型
export interface PaginatedResponse<T = any> {
  success: boolean
  data: T[]
  page: number
  page_size: number
  total: number
  message?: string
}

// 模型列表响应
export interface ModelListResponse {
  success: boolean
  data: ModelItem[]
  message?: string
}

// 模型详情响应
export interface ModelDetailResponse {
  success: boolean
  data: ModelItem
  message?: string
}

// 创建模型响应
export interface CreateModelResponse {
  success: boolean
  data: ModelItem
  message?: string
}

// 更新模型响应
export interface UpdateModelResponse {
  success: boolean
  data: ModelItem
  message?: string
}

// 删除模型响应
export interface DeleteModelResponse {
  success: boolean
  message: string
}

// 模型类型选项
export interface ModelTypeOption {
  value: ModelType
  label: string
  icon: string
  description: string
}

// 模型状态选项
export interface ModelStatusOption {
  value: ModelStatus
  label: string
  color: string
}

// 模型来源选项
export interface ModelSourceOption {
  value: ModelSource
  label: string
}

// 模型筛选条件
export interface ModelFilter {
  type?: ModelType | 'all'
  status?: ModelStatus | 'all'
  source?: ModelSource | 'all'
  keyword?: string
}

// 模型表单数据
export interface ModelFormData {
  name: string
  type: ModelType
  source: ModelSource
  description: string
  parameters: ModelParameters
  is_default: boolean
}

// 模型卡片数据
export interface ModelCardData {
  model: ModelItem
  loading?: boolean
  actions?: {
    canEdit: boolean
    canDelete: boolean
    canSetDefault: boolean
  }
}

// 模型统计信息
export interface ModelStatistics {
  total: number
  active: number
  downloading: number
  inactive: number
  byType: Record<ModelType, number>
  bySource: Record<ModelSource, number>
}

// 模型操作日志
export interface ModelOperationLog {
  id: string
  model_id: string
  operation: 'create' | 'update' | 'delete' | 'activate' | 'deactivate'
  operator: string
  timestamp: string
  details: Record<string, any>
}

// 模型配置验证规则
export interface ModelValidationRule {
  type: ModelType
  required: string[]
  optional: string[]
  validation: Record<string, {
    required?: boolean
    type?: 'string' | 'number' | 'boolean'
    min?: number
    max?: number
    pattern?: RegExp
    message?: string
  }>
}

// 模型导入/导出数据
export interface ModelExportData {
  models: Omit<ModelItem, 'id' | 'tenant_id' | 'created_at' | 'updated_at' | 'deleted_at'>[]
  export_time: string
  version: string
}

export interface ModelImportData {
  models: Omit<ModelItem, 'id' | 'tenant_id' | 'created_at' | 'updated_at' | 'deleted_at'>[]
  overwrite_existing: boolean
}

// 常量定义
export const MODEL_TYPES: ModelTypeOption[] = [
  {
    value: 'KnowledgeQA',
    label: '对话模型',
    icon: 'chat',
    description: '用于对话和问答的大型语言模型'
  },
  {
    value: 'Embedding',
    label: '嵌入模型',
    icon: 'layers',
    description: '用于文本向量化的嵌入模型'
  },
  {
    value: 'Rerank',
    label: '排序模型',
    icon: 'sort',
    description: '用于结果重排序的模型'
  }
]

export const MODEL_STATUS: ModelStatusOption[] = [
  {
    value: 'active',
    label: '活跃',
    color: 'success'
  },
  {
    value: 'downloading',
    label: '下载中',
    color: 'warning'
  },
  {
    value: 'inactive',
    label: '未激活',
    color: 'default'
  }
]

export const MODEL_SOURCES: ModelSourceOption[] = [
  {
    value: 'local',
    label: '本地'
  },
  {
    value: 'cloud',
    label: '云端'
  }
]

// 默认参数
export const DEFAULT_EMBEDDING_PARAMETERS: EmbeddingParameters = {
  dimension: 768,
  truncate_prompt_tokens: 0
}

export const DEFAULT_MODEL_PARAMETERS: ModelParameters = {
  base_url: '',
  api_key: '',
  embedding_parameters: DEFAULT_EMBEDDING_PARAMETERS
}

// 验证规则
export const MODEL_VALIDATION_RULES: Record<ModelType, ModelValidationRule> = {
  KnowledgeQA: {
    required: ['name', 'type', 'source'],
    optional: ['description', 'base_url', 'api_key'],
    validation: {
      name: {
        required: true,
        type: 'string',
        min: 1,
        max: 100,
        message: '模型名称长度为1-100个字符'
      },
      type: {
        required: true,
        type: 'string',
        message: '请选择模型类型'
      },
      source: {
        required: true,
        type: 'string',
        message: '请选择模型来源'
      },
      description: {
        required: false,
        type: 'string',
        max: 500,
        message: '描述长度不能超过500个字符'
      },
      base_url: {
        required: false,
        type: 'string',
        pattern: /^https?:\/\/.+/,
        message: '请输入有效的URL'
      },
      api_key: {
        required: false,
        type: 'string',
        min: 1,
        message: 'API Key不能为空'
      }
    }
  },
  Embedding: {
    required: ['name', 'type', 'source', 'dimension'],
    optional: ['description', 'base_url', 'api_key', 'truncate_prompt_tokens'],
    validation: {
      name: {
        required: true,
        type: 'string',
        min: 1,
        max: 100,
        message: '模型名称长度为1-100个字符'
      },
      type: {
        required: true,
        type: 'string',
        message: '请选择模型类型'
      },
      source: {
        required: true,
        type: 'string',
        message: '请选择模型来源'
      },
      description: {
        required: false,
        type: 'string',
        max: 500,
        message: '描述长度不能超过500个字符'
      },
      base_url: {
        required: false,
        type: 'string',
        pattern: /^https?:\/\/.+/,
        message: '请输入有效的URL'
      },
      api_key: {
        required: false,
        type: 'string',
        min: 1,
        message: 'API Key不能为空'
      },
      dimension: {
        required: true,
        type: 'number',
        min: 1,
        max: 10000,
        message: '向量维度范围为1-10000'
      },
      truncate_prompt_tokens: {
        required: false,
        type: 'number',
        min: 0,
        max: 100000,
        message: '截断令牌数范围为0-100000'
      }
    }
  },
  Rerank: {
    required: ['name', 'type', 'source'],
    optional: ['description', 'base_url', 'api_key'],
    validation: {
      name: {
        required: true,
        type: 'string',
        min: 1,
        max: 100,
        message: '模型名称长度为1-100个字符'
      },
      type: {
        required: true,
        type: 'string',
        message: '请选择模型类型'
      },
      source: {
        required: true,
        type: 'string',
        message: '请选择模型来源'
      },
      description: {
        required: false,
        type: 'string',
        max: 500,
        message: '描述长度不能超过500个字符'
      },
      base_url: {
        required: false,
        type: 'string',
        pattern: /^https?:\/\/.+/,
        message: '请输入有效的URL'
      },
      api_key: {
        required: false,
        type: 'string',
        min: 1,
        message: 'API Key不能为空'
      }
    }
  }
}
